package com.xcdh.learn.util;


import com.xcdh.learn.enums.DateFormat;
import com.xcdh.learn.enums.DateType;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

/**
 * Date: getTime() 、setTime()
 * DateFormat:  getInstance()、getDateInstance()、getDateTimeInstance()、getTimeInstance()
 * SimpleDateFormat:    format(Date)、 parse(String s)
 * Calendar:　 getInstance()、set() 、get()、getActualMaximum()、add()、gettime()、setTime(Date)
 */
public class DateUtil {

    public static void main(String[] args) {

        System.out.println("今天===" + toDay());
        System.out.println("一年前==" + dateCalc(null, null, -1)); //
        System.out.println("一年后==" + dateCalc(null, null, 1)); //
        System.out.println("一月前==" + dateCalc(null, DateType.MONTH, -1)); //
        System.out.println("一月后==" + dateCalc(null, DateType.MONTH, 1)); //
        System.out.println("一天前==" + dateCalc(null, DateType.DAY, -1)); //
        System.out.println("一天后==" + dateCalc(null, DateType.DAY, 1)); //
        System.out.println("一小时前==" + dateCalc(null, DateType.HOUR, -1)); //
        System.out.println("一小时后==" + dateCalc(null, DateType.HOUR, 1)); //
        System.out.println("一分钟前==" + dateCalc(null, DateType.MINUTE, -1)); //
        System.out.println("一分钟后==" + dateCalc(null, DateType.MINUTE, 1)); //

        System.out.println("一周前==" + dateCalc(null, DateType.WEEK_OF_YEAR, -1)); //
        System.out.println("一周后==" + dateCalc(null, DateType.WEEK_OF_YEAR, 1)); //


        System.out.println("WEEK_OF_MONTH==" + dateCalc(null, DateType.WEEK_OF_MONTH, -1)); //
        System.out.println("WEEK_OF_MONTH==" + dateCalc(null, DateType.WEEK_OF_MONTH, 1)); //

        System.out.println("DAY_OF_YEAR==" + dateCalc(null, DateType.DAY_OF_YEAR, -1)); //
        System.out.println("DAY_OF_YEAR==" + dateCalc(null, DateType.DAY_OF_YEAR, 1)); //

        System.out.println("当月最后一天==" + getLastDay(new Date())); //WEEK_OF_MONTH
    }

    /**
     * 今天
     *
     * @return
     */
    public static String toDay() {
        LocalDate today = LocalDate.now();
        System.out.println("Today's Local date : " + today);
        return today.toString();
    }

    /**
     * 是否闰年
     */
    public boolean isLeapYear(int year) {
        LocalDate date = LocalDate.of(year, 1, 1);
        return date.isLeapYear();
    }

    /**
     * 日期格式化
     *
     * @param date
     * @param format
     * @return
     */
    public static String format(Date date, DateFormat format) {
        SimpleDateFormat df = new SimpleDateFormat(format != null ? format.format : DateFormat.DATE_TIME.format);
        return df.format(date == null ? new Date() : date);
    }

    public static String format(Date date) {
        return format(date, DateFormat.DATE_TIME);
    }

    public static String format(DateFormat format) {
        return format(null, format);
    }

    public static String format() {
        return format(null, DateFormat.DATE_TIME);
    }


    /**
     * 日期转换
     *
     * @param source
     * @param format
     * @return
     */
    public static Date parse(String source, DateFormat format) {
        SimpleDateFormat df = new SimpleDateFormat(format != null ? format.format : DateFormat.DATE_TIME.format);
        try {
            return df.parse(StringUtils.isEmpty(source) ? format() : source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse(String source) {
        return parse(source, DateFormat.DATE_TIME);
    }

    public static Date parse() {
        return parse(null, DateFormat.DATE_TIME);
    }


    /**
     * 日期计算
     *
     * @param date
     * @param type
     * @param operand
     * @return
     */
    public static String dateCalc(Date date, DateType type, int operand) {
        Calendar specialDate = Calendar.getInstance();
        specialDate.setTime(date == null ? new Date() : date);
        specialDate.add(type == null ? DateType.YEAR.value : type.value, operand); //特定时间的1年后
        return format(specialDate.getTime());
    }

    public static String dateCalc(Date date, int operand) {
        return dateCalc(date, DateType.YEAR, operand);
    }

    public static String dateCalc(int operand) {
        return dateCalc(null, DateType.YEAR, operand);
    }

    /**
     * 获取最后一天
     *
     * @param date
     * @return
     */
    public static String getLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date == null ? new Date() : date);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return format(calendar.getTime());
    }
}
